<template>
  <a-card :bordered='false'>
    <div class='card-title'>{{ $route.meta.title }}</div>
    <div class='table-operator'>
      <a-button v-action:add type='primary' icon='plus' @click='handleAdd'>添加</a-button>
      <div style='margin: 10px 0'>
        关键词搜索：
        <a-input-search
          style='max-width: 360px; min-width: 150px;'
          v-model='queryParam.search'
          placeholder='请输入关键词'
          @search='onSearch'
        />
        <span style='padding: 0 10px'></span>
        录入时间：
        <a-range-picker format='YYYY-MM-DD' v-model='queryParam.create_time' @change='handleRefresh(true)' />
      </div>
    </div>
    <s-table
      ref='table'
      rowKey='pct_id'
      :loading='isLoading'
      :columns='columns'
      :data='loadData'
      :pageSize='15'
      :scroll='{x:1220}'
    >
      <div slot='type_name' slot-scope='text, item'>
        {{ text || '-' }}
        <a-tag color='blue' v-if='item.is_default==1'>默认</a-tag>
      </div>
      <div slot='type_desc' slot-scope='text, item'>
        {{ text || '-' }}
      </div>
      <div slot='status' slot-scope='text, item'>
        <a-tag v-if='text==0' color='blue'>启用</a-tag>
        <a-tag v-if='text==1'>停用</a-tag>
      </div>
      <div slot='not_log' slot-scope='text, item'>
        <a-tag v-if='text==1' color='blue'>是</a-tag>
        <a-tag v-if='text==0'>否</a-tag>
      </div>
      <div class='actions' slot='action' slot-scope='text, item'>
        <a v-action:edit @click='handleEdit(item)'>编辑</a>
        <a v-action:delete @click='handleDelete(item)'>删除</a>
      </div>
    </s-table>
    <SaveForm ref='SaveForm' @handleSubmit='handleRefresh' />
  </a-card>
</template>

<script>
import * as Api from '@/api/presales_customer/type'
import { STable } from '@/components'
import SaveForm from './modules/SaveForm.vue'
// import EditForm from './modules/EditForm'

export default {
  name: 'Index',
  components: {
    STable,
    SaveForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        search: '',
        create_time: []
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: 'ID',
          width: 60,
          dataIndex: 'pct_id'
        },
        {
          title: '类型名称',
          width: 100,
          dataIndex: 'type_name',
          scopedSlots: { customRender: 'type_name' }
        },
        {
          title: '类型描述',
          dataIndex: 'type_desc',
          scopedSlots: { customRender: 'type_desc' }
        },
        {
          title: '无需跟进',
          dataIndex: 'not_log',
          width: 180,
          scopedSlots: { customRender: 'not_log' }
        },
        {
          title: '当前状态',
          dataIndex: 'status',
          width: 180,
          scopedSlots: { customRender: 'status' }
        },
        {
          width: 180,
          title: '录入时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created() {
  },
  methods: {
    // 新增记录
    handleAdd() {
      this.$refs.SaveForm.add()
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.SaveForm.edit(item)
    },

    // 删除记录
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted({ apiId: item['pct_id'] })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 检索查询
    onSearch() {
      this.handleRefresh(true)
    }

  }
}
</script>
